package com.yuda.tools.authreport.controller;

import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.tools.authreport.domain.vo.OrderExcel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.net.ftp.FTP;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
public class AuthReportController {

    /**
     * 导入excel表格，
     * 根据导入的excel表格从已经生成的pdf文件堆中，把拎出来保存到新的文件夹中
     */
    @PreAuthorize(hasPermi = "toolsystem:filter:import")
    @ApiOperation("此接口数据很大，会比较慢，甚至数分钟才能处理完成")
    @PostMapping("/generatorPdfByExcel")
    public synchronized AjaxResult generatorAuthPdfByExcel(MultipartFile multipartFile) {

        LocalDateTime now = LocalDateTime.now();
        String dirName = "/mnt/auth_pdf/download/" + now.getYear() + "" + now.getMonthValue() + "" + now.getDayOfMonth() + "" + now.getHour() + "" + now.getMinute() + "";

        try {
            InputStream inputStream = multipartFile.getInputStream();
            ExcelUtil<OrderExcel> util = new ExcelUtil<OrderExcel>(OrderExcel.class);
            List<OrderExcel> orderExcels = util.importExcel(inputStream);
            //1.获取到订单之后，创建新的目录
            File file = new File(dirName);
            if (!file.isDirectory()) {
                file.mkdirs();
            }

            //2.复制pdf 到新的目录中
            String examUuid;
            String idCard;
            String examType;
            for (OrderExcel orderExcel : orderExcels) {
                examUuid = orderExcel.getExamUuid();
                idCard = orderExcel.getIdCard();
                if (examUuid.startsWith("TACA")) {
                    examType = examUuid;
                } else {
                    examType = "TDA" + examUuid.substring(7, 15);
                }
                FileCopyUtils.copy(new File("/mnt/auth_pdf/" + examUuid + "/" + idCard + ".pdf"), new File(dirName + "/" + examType + orderExcel.getProvince() + orderExcel.getName() + idCard.substring(idCard.length() - 6) + ".pdf"));
                System.out.println("拷贝文件/mnt/auth_pdf/" + examUuid + "/" + idCard + ".pdf 结束");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("需要下载的文件的路径为: " + dirName);
    }


    @ApiOperation("列举出 /mnt/auth_pdf/download/ 下边的文件夹")
    @GetMapping("/listFolder")
    public List<String> listFolder() {

        List<String> list = new ArrayList<>();
        File file = new File("/mnt/auth_pdf/download/");
        if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    list.add(file1.getName());
                }
            }
        }

        return list;
    }


    @ApiOperation("删除 /mnt/auth_pdf/download/ 下边的文件夹")
    @PreAuthorize(hasPermi = "toolsystem:filter:remove")
    @DeleteMapping("/deleteFolder/{folderName}")
    public AjaxResult deleteFolder(@PathVariable("folderName") String folderName) {

        File file = new File("/mnt/auth_pdf/download/" + folderName);
        File[] files = file.listFiles();
        for (File file1 : files) {
            file1.delete();
        }
        file.delete();
        return AjaxResult.success("删除完成");
    }


}
